package com.inxedu.os.edu.controller.article;

import com.google.gson.Gson;
import com.inxedu.os.common.util.QiNiuUtil;
import com.inxedu.os.edu.pojo.PictureResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping({"/image"})
@Api(tags = "图片上传")
public class AddUpload {
    @RequestMapping(value = {"/gok4"},method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public String fileUpload(@RequestParam("uploadfile") MultipartFile uploadfile, HttpServletRequest request) throws IOException {
        PictureResult result=new PictureResult();
        Gson gson=new Gson();
        FileInputStream filename = (FileInputStream)uploadfile.getInputStream();
        //生成随机名称
        String saveFileName = UUID.randomUUID().toString().replace("-","");
        QiNiuUtil.upload2Qiniu(filename,saveFileName);
        result.setError(0);
        result.setMessage("上传成功");
        result.setUrl(QiNiuUtil.qiniu_img_url_pre+saveFileName);
        request.getSession().setAttribute("uurl",QiNiuUtil.qiniu_img_url_pre+saveFileName);
        String json = gson.toJson(result);
        return json;
    }
}